package cc.fs.kotlin.ui.activity

import android.app.Fragment
import android.os.Bundle
import android.os.Message
import android.support.v4.view.ViewPager
import cc.fs.kotlin.R
import cc.fs.kotlin.ui.activity.helper.BaseActivity
import cc.fs.kotlin.ui.adapter.MyFragmentPagerAdapter
import cc.fs.kotlin.ui.fragment.BottomFragment
import cc.fs.kotlin.ui.fragment.HomeFrament
import cc.fs.kotlin.ui.fragment.MessageFragment
import cc.fs.kotlin.ui.fragment.ProfileFragment
import cc.fs.kotlin.utils.bindView

/**
 * Created by fostion on 2/19/16.
 */
class MainActivity : BaseActivity() , BottomFragment.TabListener{
    var bottomTabFragment:BottomFragment ?= null
    val viewPager: ViewPager by bindView(R.id.viewPager)
    var myFragmentPagerAdapter: MyFragmentPagerAdapter ? = null
    var homeFragment: HomeFrament ? = null
    var messageFragment: MessageFragment ? = null
    var profileFragment: ProfileFragment ? = null
    val titles:Array<String> = arrayOf("home","message","profile")
    val classes:Array<Class<*>> = arrayOf(HomeFrament::class.java,MessageFragment::class.java,ProfileFragment::class.java)
    val bundles:Array<Bundle> = arrayOf(Bundle(), Bundle(), Bundle())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_main)

        setUpFragment()
    }

    fun setUpFragment() {
        myFragmentPagerAdapter = MyFragmentPagerAdapter(this@MainActivity,titles,classes,bundles)
        viewPager.adapter = myFragmentPagerAdapter
        viewPager.addOnPageChangeListener(object:ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                bottomTabFragment?.setNowSelected(position)
            }
        })

        bottomTabFragment = fragmentManager.findFragmentById(R.id.fragmentBottomTab) as BottomFragment
    }

    override fun handler(msg: Message) {

    }

    override fun onTabChange(position: Int) {
        viewPager.setCurrentItem(position,false)
    }

    override fun onTabReClick(position: Int) {
    }

}
